from rest_framework.views import APIView
from utils.config import Config
from utils.result import R
from utils.score import GiveScoreSys
from utils.aliverify_code import * 
from api.models import ItEduCommentLesson,ItEduLessonVideo,ItEduUser,ItEduReplyComment,ItEduLessonVideoSet

class UserCommentAPIView(APIView):

    """
    获取用户自己的评论（前5条）
    """
    def get(self,request,*args, **kwargs):

        user_pk = request.session.get("user_pk")

        comment_list = list(ItEduCommentLesson.objects.filter(user___id=user_pk).order_by("-_id")[:5].values())

        return R().data_result(comment_list = comment_list)

class CommentAPIView(APIView):

    """
    获取视频集的评论
    """
    def put(self,request,*args, **kwargs):

        offset = int(request.GET.get("offset"))
        lesson_set_id = int(request.GET.get("lesson_set_id")) 

        lesson_set = ItEduLessonVideoSet.objects.get(pk=lesson_set_id)
        video_id_list = lesson_set.lesson_order.split(",")

        comment_list = list(ItEduCommentLesson.objects.filter(lesson___id__in=video_id_list,lesson_score__get=Config.MIN_SHOW_LESSON_SET_SCORE).order_by("-lesson_score","-_id")[offset:offset+5].values(
            "user__avatar_image",
            "user__nickname",
            "lesson_score",
            "content",
            "createtime",
            "_id"
        ))

        for comment in comment_list:
                comment["reply_num"] = ItEduReplyComment.objects.filter(comment_lesson___id = comment["_id"]).count()

        return R().data_result(comment_list = comment_list)
    
    """
    获取视频对应的评论
    """
    def get(self,request):
        
        offset = int(request.GET.get("offset"))
        video_id = int(request.GET.get("video_id"))

        comment_list = list(ItEduCommentLesson.objects.filter(lesson___id=video_id,lesson_score__get=Config.MIN_SHOW_LESSON_SET_SCORE).order_by("-lesson_score","-_id")[offset:offset+5].values(
            "user__avatar_image",
            "user__nickname",
            "lesson_score",
            "content",
            "createtime",
            "_id"
        ))

        for comment in comment_list:
            comment["reply_num"] = ItEduReplyComment.objects.filter(comment_lesson___id = comment["_id"]).count()

        return R().data_result(comment_list = comment_list)
    

    """
    用户对视频发起评论
    """
    def post(self,request,*args, **kwargs):

        user_pk = request.session.get("user_pk")
        video_id = request.data.get("video_id")
        content = request.data.get("content")
        score = request.data.get("score")# 对该视频打分

        video = ItEduLessonVideo.objects.get(pk=video_id)
        user = ItEduUser.objects.get(pk=user_pk)

        video.score = GiveScoreSys.calculate_add_score(video.score,score,user.user_info.credit)

        comment = ItEduCommentLesson.objects.create(
            user = user,
            lesson_score = score,
            content = content,
            lesson = video
        )

        video.save()

        return R().data_result(comment_id = comment._id)
        
        
    
    